package request

import (
	"bytes"
	"io"
	"io/ioutil"
	"net/http"
)

func Request(method, url string, header http.Header, data io.Reader) ([]byte, error) {
	client := &http.Client{}
	req, err := http.NewRequest(method, url, data)
	if err != nil {
		return []byte{}, err
	}
	if header != nil {
		req.Header = header
	}
	resp, err := client.Do(req)
	if err != nil {
		return []byte{}, err
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return []byte{}, err
	}
	return body, nil
}

func Get(url string, header http.Header) ([]byte, error) {
	return Request(`GET`, url, header, nil)
}

func Post(url string, header http.Header, data []byte) ([]byte, error) {
	return Request(`POST`, url, header, bytes.NewReader(data))
}
